﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Qi.Data.Contracts;
using Qi.Data.EntityFramework;
using System.Web;
using Qi.Data;

namespace Standards.Web.Api
{
    using System.Linq.Expressions;
    using Standards.Data;
    using Standards.Web.Attributes;
    using Standards.Contracts;
    using Standards.Model.Models;

    public class RolesController : ApiController
    {
        private IStandardsUnitOfWork unitOfWork;

        public RolesController(IStandardsUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }
    
        [Queryable]
        [HasPermission(Permission.CanViewRoles)]
        public IQueryable<RoleDto> GetRoles()
        {
            var roles = unitOfWork.Roles.GetAll(r => r.Permissions);
            return roles;
        }

        //public RoleDto GetRole(int id)
        //{
        //    var role = this.rolesRepository.Get(id);
        //    if (role == null)
        //    {
        //        throw new HttpResponseException(HttpStatusCode.NotFound);
        //    }

        //    return role;
        //}

        //// Add
        //public void PostRole(RoleDto role)
        //{
        //    this.rolesRepository.Add(role);
        //}

        //// Update
        ////TODO: should update take id and entity?
        //public void PutRole(int id, RoleDto role)
        //{
        //    role.Id = id;
        //    this.rolesRepository.Update(role);
        //}

        //public void DeleteRole(int id)
        //{
        //    this.rolesRepository.Delete(id);
        //}
    }



    public class TestRepository<TId, TEntity> : IRepository<TId, TEntity>
        where TEntity : class, IEntity<TId>
    {
        private IDictionary<TId, TEntity> entities;

        public TestRepository()
        {
            //HttpContext context = HttpContext.Current;
            //if (context != null)
            //{
            //    this.entities = context.Session["Entities"] as Dictionary<TId, TEntity>;
            //    return;
            //}

            this.entities = new Dictionary<TId, TEntity>();
        }

        public TEntity Get(TId id)
        {
            if (this.entities.ContainsKey(id))
            {
                return this.entities[id];
            }

            return default(TEntity);
        }

        public IQueryable<TEntity> GetAll(params Expression<Func<TEntity, object>>[] includes)
        {
            return this.entities.Values.AsQueryable();
        }

        public void Add(TEntity entity)
        {
            if (this.entities.ContainsKey(entity.Id))
            {
                throw new InvalidOperationException(
                    string.Format("The entities collection already contains the entity with id '{0}'", entity.Id));
            }

            this.entities.Add(entity.Id, entity);
        }

        public void Delete(TId id)
        {
            if (!this.entities.ContainsKey(id))
            {
                throw new InvalidOperationException(
                    string.Format("The entity with id '{0}' does not exist in the entities collection", id));
            }

            this.entities.Remove(id);
        }

        public void Delete(TEntity entity)
        {
            this.Delete(entity.Id);
        }

        public void Update(TEntity entity)
        {
            if (!this.entities.ContainsKey(entity.Id))
            {
                throw new InvalidOperationException(
                    string.Format("The entity with id '{0}' does not exist in the entities collection", entity.Id));
            }

            this.entities[entity.Id] = entity;
        }
    }
}